<?php
set_time_limit(24*86400);
ini_set('memory_limit', '256M');
require '../home/config.php';
ob_clean();
?>
<meta http-equiv="Content-Type" content="text/html"; charset="utf-8">
<?php
error_reporting(null);
$db_law = new db_query("SELECT rss_id,rss_link,rss_user_id,rss_group_id
                        FROM ss_rss
                        WHERE rss_active = 1");
$law = $db_law->resultArray();
unset($db_law);
$total = count($law);
$count = getValue('c','int','GET',0);
if($count >= $total){
    die('Đã lấy hết các tin từ rss');
}
while ($count < $total) {
    $rss_id = $law[$count]['rss_id'];
    $rss_link = $law[$count]['rss_link'];
    $rss_user_id = $law[$count]['rss_user_id'];
    $rss_group_id = $law[$count]['rss_group_id'];
    $rss = array();
    try{
        $doc = new DOMDocument(); 
        if($doc->load($rss_link)){
            $count_item_news = 0;
            foreach ($doc->getElementsByTagName('item') as $node) {
                $count_item_news ++;
                if($count_item_news > 10) break;
                $itemRSS = array ( 
                    'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 
                    //'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue, 
                    //'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
                    //'image'=>$node->getElementsByTagName('image')->item(0)->nodeValue,
                    'link' => $node->getElementsByTagName('link')->item(0)->nodeValue
                ); 
                array_push($rss, $itemRSS);
            };
        };
    }
    catch(exception $e){
        $rss = '';
        $e->getMessage();
    };
    if($rss){
        foreach($rss as $key=>$item){
            //descriptions
            //$description = trim($item['desc']);
            //lay title
            $title = $item['title'];
            //thoi gian chen vao csdl
            //$time_insert = time();
            //link item
            $link = $item['link'];
            //thoi gian public
            //$time_public = ($item['date'])?$item['date']: '';
            //$time_public = $time_public ? StringToTime($time_public) : time();
            //ma check sum kiem tra trung tin
            //$checksum = md5($title.$link);
              //Xử lý description
              /*
            if($description){
                $simpleHtml = str_get_html(htmlspecialchars_decode($description));
                //Ảnh của description
                $img = $item['image'] ? $item['image'] : ($simpleHtml->find('img',0) ? $simpleHtml->find('img',0)->src : '');
                //Nội dung của description
                $desc = $simpleHtml->plaintext;
            }*/
            //Bắt đầu insert 
            $error = '';
            $lin_count_run = 0;
            $lin_member_run = 0;
            $myform = new generate_form();
            $myform->addTable('ss_link');
            $myform->removeHTML(0);
            $myform->add('lin_title','title',0,1,'',0,'',0,'');
            $myform->add('lin_value','link',0,1,'',0,'',1,'Trung link');
            $myform->add('lin_rss_id','rss_id',1,1,0);
            $myform->add('lin_user_id','rss_user_id',1,1,0);
            $myform->add('lin_group_id','rss_group_id',1,1,0);
            $myform->add('lin_count_run','lin_count_run',1,1,0);
            $myform->add('lin_member_run','lin_member_run',1,1,0);
            $error .= $myform->checkdata();
            if($error == ''){
                $db_insert = new db_execute_return();
                $last_id = $db_insert->db_execute($myform->generate_insert_SQL()); 
                if($last_id > 0) {
                  echo 'Lay thanh cong link: <b>' .$link . '</b><br />';
                }               
            }else {
                sleep(1);
            }
        }
    }
    
    if(!@$last_id){
        echo 'Hết tin để lấy';
    }
    $count++;
    sleep(2);
    redirect('getNews.php?c='.$count);
}
?>